<script>
export default {
  name: "KeFuView",
  data() {
    return {
      token:''
    }
  },
  methods: {
    onClickLeft() {
      this.$router.push({ name: 'onchat', params: { id: this.token }})
    },
    getBotResponse(input) {
      const lowerCaseInput = input.toLowerCase();
      if (lowerCaseInput.includes("你好")) {
        return "你好,有什么需要帮助的吗?";
      } else if (lowerCaseInput.includes("请问一下!这里的价格怎么样")) {
        return "这里的价格亲民实惠!";
      } else if (lowerCaseInput.includes("退款")) {
        return "好的,亲,这就为你联系卖家";
      } else if (lowerCaseInput.includes("查看订单")) {
        return "查看订单在我的里面有一个全部订单可以看";
      } else {
        return "抱歉没听清你在说啥";
      }
    },
    sendMessage(){
      const userInput = document.getElementById('userInput').value;
      const chatBox = document.getElementById('chatBox');

      if (userInput.trim() === "") return;

      // Display user message
      const userMessageDiv = document.createElement('div');
      userMessageDiv.className = 'message user-message';
      userMessageDiv.textContent = userInput;
      chatBox.appendChild(userMessageDiv);

      // Clear the input field
      document.getElementById('userInput').value = '';

      // Simulate bot response
      setTimeout(() => {
        const botResponse = this.getBotResponse(userInput);
        const botMessageDiv = document.createElement('div');
        botMessageDiv.className = 'message bot-message';
        botMessageDiv.textContent = botResponse;
        chatBox.appendChild(botMessageDiv);
        chatBox.scrollTop = chatBox.scrollHeight; // Scroll to bottom
      }, 500);
    }

  },
  created() {
    this.token=localStorage.getItem('token')
    if (this.token == null) {
      this.$router.push('/')
    }
  }
}
</script>

<template>
  <div class="container">
    <van-icon name="arrow-left" @click="onClickLeft"/>
    <div class="chat-box" id="chatBox">
      <div class="message bot-message">Hello! How can I assist you today?</div>
    </div>
    <input type="text" id="userInput" placeholder="Type your message...">
    <button @click="sendMessage()">Send</button>
  </div>

</template>

<style scoped>
.container {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  width: 400px;
  max-height: 600px;
  overflow-y: auto;
}
h1 {
  color: #333;
  text-align: center;
}
.chat-box {
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 10px;
  margin-bottom: 10px;
  height: 300px;
  overflow-y: auto;
}
.message {
  margin: 10px 0;
}
.user-message {
  text-align: right;
  color: #007bff;
}
.bot-message {
  text-align: left;
  color: #555;
}
input[type="text"] {
  width: calc(100% - 22px);
  padding: 10px;
  margin: 10px 0;
  border: 1px solid #ccc;
  border-radius: 4px;
}
button {
  width: 100%;
  padding: 10px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
button:hover {
  background-color: #0056b3;
}
</style>